﻿#ifndef THREADFUNCTION_H
#define THREADFUNCTION_H

#include <functional>

#include <QObject>

class ThreadRunner : public QObject
{
    Q_OBJECT
public:
    explicit ThreadRunner(bool autoDelete = false);
    explicit ThreadRunner(std::function<void()> fun, bool autoDelete = false);

public slots:
    void start();
    void startThread();
    void startThreadWithBlock();

signals:
    void finshed();

protected:
    virtual void run();

protected:
    std::function<void()> m_fun;
    bool m_bAutoDelete;
};

namespace Custom
{
    void runInMainThread(std::function<void()> fun, bool block = true);
    void runInTargetThread(std::function<void()> fun, QObject *target, bool block = true);
    void startThread(std::function<void()> fun, bool block = true);
    void startThread(ThreadRunner *runner, bool block = true);//runner会被析构
}

#endif // THREADFUNCTION_H
